$(function () {

    initWholeTree();

    //确定添加
    $("#menuAddBtn").click(function () {
        // 收集表单填写的数据
        var name = $.trim($("#menuAddModal [name='name']").val());
        var url = $.trim($("#menuAddModal [name='url']").val());
        var icon = $("#menuAddModal [name='icon']:checked").val();
        if(name == null || name == "") {
            layer.msg("请填写菜单项名称！");
            return ;
        }

        if(url == null || url == "") {
            layer.msg("请填写菜单项对应的访问地址！");
            return ;
        }
        // 发送Ajax请求
        // 发送Ajax请求
        $.ajax({
            "url":"/menu/save",
            "type":"post",
            "dataType":"json",
            "data":{
                "name":name,
                "url":url,
                "pid":window.menuId,   // 当前操作的节点是新节点的父节点
                "icon":icon
            },
            "success":function(response){

                var result = response.result;

                if(result == "SUCCESS") {
                    layer.msg("操作成功！");

                    initWholeTree();
                }

                if(result == "FAILED") {
                    layer.msg(response.message);
                }

            },
            "error":function(response){
                layer.msg(response.message);
            }
        });

        $("#menuAddModal").modal("hide");
        initWholeTree();
    });

    //确定修改
    $("#menuEditBtn").click(function(){

        // 收集表单填写的数据
        var name = $.trim($("#menuEditModal [name='name']").val());
        var url = $.trim($("#menuEditModal [name='url']").val());
        var icon = $("#menuEditModal [name='icon']:checked").val();

        if(name == null || name == "") {
            layer.msg("请填写菜单项名称！");
            return ;
        }

        if(url == null || url == "") {
            layer.msg("请填写菜单项对应的访问地址！");
            return ;
        }

        // 发送Ajax请求
        $.ajax({
            "url":"/menu/update",
            "type":"post",
            "dataType":"json",
            "data":{
                "id":window.menuId,
                "name":name,
                "url":url,
                "icon":icon
            },
            "success":function(response){

                var result = response.result;

                if(result == "SUCCESS") {
                    layer.msg("操作成功！");

                    initWholeTree();
                }

                if(result == "FAILED") {
                    layer.msg(response.message);
                }

            },
            "error":function(response){
                layer.msg(response.message);
            }
        });
        $("#menuEditModal").modal("hide");
        initWholeTree();
    });






});

function initWholeTree() {
    var setting = {
        "view": {
            //设置自定义图标
            "addDiyDom": showMyIcon,
            "addHoverDom": addHoverDom,
            "removeHoverDom": removeHoverDom
        },
        "data": {
            "key": {
                "url": "notExistsProperty" // 阻止点击节点后跳转
            }
        }
    };

    $.ajax({
        "url":"/menu/get/whole/tree",
        "type":"post",
        "dataType":"json",
        "success":function(ajaxResult){
            if(ajaxResult.result=="SUCCESS"){
                var zNodes=ajaxResult.data;
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            }else{
                layer.msg("加载菜单数据失败！原因是："+response.message);
            }
        },
        "error":function(response){
            layer.msg("加载菜单数据失败！原因是："+response.message);
        }
    });
}


//更换图标
function showMyIcon(treeId, treeNode) {
    console.log(treeNode.tId);
    console.log(treeNode.id);
    console.log(treeNode.icon);
    console.log(treeNode.name);
    console.log(treeNode.level);

    var newClass=treeNode.icon;
    var spanid=treeNode.tId+"_ico";
    $('#'+spanid).removeClass().addClass(newClass).css("background","");
}
//生成按钮组
function generateBtnGrp(treeNode){
    //treeDemo_20_btnGrp
    var btnGrpSpanId= treeNode.tId+"_btnGrp";
    var menuId=treeNode.id;

    // 声明三种按钮
    var addBtn = "<a onclick='showAddModal(this)' id='"+menuId+"' class='btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' title='添加子节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-plus rbg'></i></a>";
    var editBtn = "<a onclick='showEditModal(this)' id='"+menuId+"' class='btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' title='编辑节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-edit rbg'></i></a>";
    var removeBtn = "<a onclick='showConfirmModal(this)' id='"+menuId+"' class='btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' title='删除节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-times rbg'></i></a>";

    // 生成span标签对应的jQuery对象
    var $span = $("<span id='"+btnGrpSpanId+"'></span>");
    //获取菜单的level
    var level=treeNode.level;
    if(level==0){
        $span.append(addBtn);
    }else if(level==1){
        if(treeNode.children.length>0){
            $span.append(addBtn+" "+editBtn);
        }else{
            $span.append(addBtn+" "+editBtn+" "+removeBtn);
        }
    }else{
        $span.append(editBtn+" "+removeBtn);
    }
    return $span;

}

//鼠标悬浮
function addHoverDom(treeId, treeNode){

    //如果按钮组存在，则不进行添加
    var btnGrpSpanId = treeNode.tId + "_btnGrp";
    if($('#'+btnGrpSpanId).length>0){
        return;
    }
    //得到按钮组
    var btnGrp=generateBtnGrp(treeNode);

    //找到菜单中的超链接
    var anchorId =treeNode.tId+"_a";
    $('#'+anchorId).append(btnGrp);
}

//鼠标移出
function removeHoverDom(treeId, treeNode){
    //找到按钮组 删除
    var btnGrpSpanId = treeNode.tId + "_btnGrp";
    $('#'+btnGrpSpanId).remove();
}

//添加菜单
function showAddModal(currentBtn) {

    // 打开模态框
    $("#menuAddModal").modal("show");

    // 将当前节点的id存入全局变量
    window.menuId = currentBtn.id;
}
//修改弹框
function showEditModal(currentBtn) {

    // 打开模态框
    $("#menuEditModal").modal("show");

    // 获取当前节点的id存入全局变量
    window.menuId = currentBtn.id;

    // 发送请求查询Menu对象
    $.ajax({
        "url":"/menu/get/"+window.menuId,
        "type":"get",
        "dataType":"json",
        "success":function(response) {

            var result = response.result;

            if(result == "SUCCESS") {

                // 从响应数据中获取Menu对象
                var menu = response.data;

                // 获取各个属性值
                var name = menu.name;

                var url = menu.url;

                var icon = menu.icon;

                // 设置各个对应的表单项
                $("#menuEditModal [name='name']").val(name);

                $("#menuEditModal [name='url']").val(url);

                // radio需要让value值和后端查询到的icon一致的设置为被选中
                $("#menuEditModal [name='icon'][value='"+icon+"']").attr("checked",true);
            }

            if(result == "FAILED") {
                layer.msg(response.message);
            }
        },
        "error":function(response) {
            layer.msg(response.message);
        }
    });

}
function showConfirmModal(currentBtn) {

    // 获取当前节点的id存入全局变量
    window.menuId = currentBtn.id;

        // 发送Ajax请求
        $.ajax({
            "url":"/menu/del/"+window.menuId,
            "type":"get",
            "dataType":"json",
            "success":function(response){

                var confirmResult = window.confirm("您真的要删除信息吗");
                if(confirmResult == true) {
                    layer.msg("删除成功！");
                    initWholeTree();
                }
            },
            "error":function(response){
                layer.msg(response.message);
            }
        });





}
